﻿using System;
using System.Collections.Generic;
using System.Data;
using IDAL.RF.Package;
using IDAL.RF.Warehouse;
using Model.RF.Package;
using Model.RF.Warehouse;

namespace BLL.RF.Package
{
    public class PackintoBox
    {
        private static readonly IPackintoBox dal = DALFactory.DataAccess.CreatePackintoBox();
        private static readonly IStockOut StockOut_dal = DALFactory.DataAccess.CreateStockOut();

        public bool PackintoBox_Insert(PackintoBoxInfo si, DataTable itemdt)
        {
            return dal.PackintoBox_Insert(si, itemdt);
        }

        /// <summary>
        /// 1.检查是否有包装记录
        /// 2.检查条码是否属于相应产品类型
        /// </summary>
        public bool PackintoBox_CheckandInsert(PackintoBoxInfo pbi, DataTable dt,bool isCheckPIM,decimal pimValue, out string message)
        {
            StockOutInfo si = new StockOutInfo { ProductTypeID = pbi.ProductTypeID, UserID = pbi.UserID };
            string invalidpackagesns = StockOut_dal.StockOut_InvalidPackageSNs(si, dt);
            if (!string.IsNullOrEmpty(invalidpackagesns))
            {
                message = "以下条码没有包装记录：" + invalidpackagesns + "不允许装箱，请检查";
                return false;
            }
            else
            {
                string invalidpackintoboxsns = StockOut_dal.StockOut_InvalidPackintoBoxSNs(si, dt);
                if (!string.IsNullOrEmpty(invalidpackintoboxsns))
                {
                    message = "以下条码已经装箱过：" + invalidpackintoboxsns + "不允许再次装箱，请检查";
                    return false;
                }
                else
                {
                    string InvalidPIDSNs = StockOut_dal.StockOut_InvalidPIDSNs(si, dt);
                    if (!string.IsNullOrEmpty(InvalidPIDSNs))
                    {
                        message = "以下条码不属于产品类型" + si.ProductTypeID + "：" + InvalidPIDSNs + "不允许装箱，请检查";
                        return false;
                    }
                    if(isCheckPIM)
                    {
                        string invalidPIMValues = dal.PackintoBox_InvalidPIMValues(pimValue, dt);
                        if (!string.IsNullOrEmpty(invalidPIMValues))
                        {
                            message = "以下条码的最后一次交调测试值大于或等于设定交调指标值" + pimValue + ":" 
                                + invalidPIMValues + "；其它条码小于设定交调指标值，无法装入一个包装箱，请检查！";
                            return false;
                        }
                    }
                }
                message = "";
                return dal.PackintoBox_Insert(pbi, dt);
            }
        }
    }
}
